package cn.zyjblogs.server.user.vo;

import cn.zyjblogs.starter.common.entity.constant.ContextKeyConstant;
import cn.zyjblogs.starter.common.entity.context.BaseContext;
import cn.zyjblogs.starter.common.entity.dto.ContextDto;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.oauth2.common.OAuth2AccessToken;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Set;

/**
 * @author zhuyijun
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class OAuth2AccessTokenVo implements Serializable {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty(value = "access token", dataType = "String", example = "abc.efg.hjk")
    private String token;

    @ApiModelProperty(value = "token类型", dataType = "String", example = "bearer")
    private String token_type;

    @ApiModelProperty(value = "刷新 token", dataType = "String", example = "abc.efg.hjk")
    private String refresh_token;

    @ApiModelProperty(value = "token过期剩余时间(秒)", dataType = "Integer", example = "43199")
    private Integer expires_in;

    @ApiModelProperty(value = "token作用域", dataType = "String", example = "server")
    private Collection<String> scope;

    @ApiModelProperty(value = "token唯一标识", dataType = "String", example = "18b82fad-0b9b-401f-a3ab-1b3592c7f267")
    private String jti;

    @ApiModelProperty(value = "用户id", dataType = "String", example = "0997eb541e59cc2d704a0f29708710fe")
    private String userId;

    @ApiModelProperty(value = "用户账号", dataType = "String", example = "hangman")
    private String username;
    @ApiModelProperty(value = "过期时间", dataType = "String", example = "hangman")
    private Date expiration;


    public OAuth2AccessTokenVo(String value, String tokenType, String refreshToken, int expiresIn, Date expiration, Set<String> scope, Map<String, Object> addition) {
        this.token = value;
        this.token_type = tokenType;
        this.refresh_token = refreshToken;
        this.expires_in = expiresIn;
        this.scope = scope;
        this.userId = (String) addition.get(ContextKeyConstant.USER_ID_KEY);
        this.username = (String) addition.get(ContextKeyConstant.USERNAME_KEY);
        this.jti = (String) addition.get("jti");
        this.expiration = expiration;
    }

    public static OAuth2AccessTokenVo TransferToken(OAuth2AccessToken token) {
        OAuth2AccessTokenVo oAuth2AccessTokenVo = new OAuth2AccessTokenVo(
                token.getValue(),
                token.getTokenType(),
                token.getRefreshToken().getValue(),
                token.getExpiresIn(),
                token.getExpiration(),
                token.getScope(),
                token.getAdditionalInformation());
        BaseContext.set(ContextDto.builder()
                .userId(oAuth2AccessTokenVo.getUserId())
                .username(oAuth2AccessTokenVo.getUsername())
                .token(oAuth2AccessTokenVo.getToken())
                .build());
        return oAuth2AccessTokenVo;
    }

}
